/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;
import Ressources.Message;
import actionForms.SaisieForm;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author alocubiche
 */
public class SaisieAction extends org.apache.struts.action.Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        
        DiscussionBean bean = null;
        ServletContext context = this.getServlet().getServletContext();
        bean = (DiscussionBean)context.getAttribute("discussion");
        if(bean == null)
        {
            bean = new DiscussionBean();
            context.setAttribute("discussion", bean);
        }
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        
        /*On enregistre un objet TODO
         * Message mess = new Message(dateFormat.format(cal.getTime()),
                                    ((SaisieForm)form).getMessage(),
                                    ((SaisieForm)form).getLogin()
                                    );
        bean.getMessages().add(mess);*/
        
        bean.getMessages().add(dateFormat.format(cal.getTime()) + " " + ((SaisieForm)form).getLogin() + " : " + ((SaisieForm)form).getMessage());    
        
        Ressources.Serialize.set(bean.getMessages());
        
        return (mapping.findForward("success"));
    }
}
